use std::ops::Deref;

use crate::{CachedID, JClassID, JEnv, JNameMaker, JObject, JPlainMarker};

pub struct JString {
	__supper: JObject,
}
impl Deref for JString {
	type Target = JObject;
	fn deref(&self) -> &Self::Target {
		&self.__supper
	}
}
impl AsRef<JObject> for JString {
	fn as_ref(&self) -> &JObject {
		&self.__supper
	}
}
impl JNameMaker for JString {
	const JAVA_PATH: &'static [u8] = b"java/lang/String\0";
}
impl JPlainMarker for JString {
	fn class(env: Option<&JEnv>) -> Option<JClassID> {
		static CACHE: CachedID = CachedID::new();
		CACHE.get(|| JEnv::env(env)?.find_class(Self::JAVA_PATH))
	}
}
impl JString {
	pub fn try_into_string(&self, env: &JEnv) -> Option<String> {
		env.get_string(self)
	}
	pub fn from_sys<'a>(obj: sys::jstring) -> Option<&'a Self> {
		if obj.is_null() {
			None
		} else {
			Some(unsafe { &*(obj as *const JString) })
		}
	}
	pub fn new(env: &JEnv, value: impl AsRef<str>) -> Option<&Self> {
		env.new_string(value)
	}
}
